package com.blog.cloud.controller.rec.upload;

import java.util.HashMap;
import java.util.Map;

import com.blog.cloud.comm.modal.dto.ResponeObj;
import com.blog.cloud.comm.modal.pojo.User;
import com.blog.cloud.controller.base.BaseController;
import com.blog.cloud.service.rec.upload.UploadService;
import com.jfinal.core.ActionKey;
import com.jfinal.upload.UploadFile;

public class UploadController extends BaseController {

	
	
	@ActionKey("toUploadJsp")
	public void toUploadJsp(){
		render(super.getResponeJspPrefix("rec_upload_file.jsp"));
	}
	
	
	
	public void uploadFile(){
		UploadFile file = getFile();
		User user = (User)getSessionAttr("user");
		String realPath = getRequest().getRealPath("/")+"data";
		String contextPathStr = getRequest().getContextPath();
		String contextPath = contextPathStr.substring(1, contextPathStr.length());
		String headerPath = realPath.replace(contextPath, "upload");
		String uploadFile = UploadService.ser.uploadFile(file.getFile() , user , headerPath);
		
		Map<String ,String > map = new HashMap<String ,String>();
		String substring = uploadFile.substring(uploadFile.indexOf("upload") , uploadFile.length());
		System.out.println("--->"+substring);
		map.put("url", "http://127.0.0.1:8080/"+substring);
		ResponeObj obj = new ResponeObj();
		
		
		
		obj.setCode("200");
		obj.setDetails(map);
		System.out.println(uploadFile);
		renderJson(obj);
	}
}
